<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title>数据批量录入</title>
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">

		<!--标准mui.css-->
		<link rel="stylesheet" href="css/mui.min.css">
		<link href="css/mui.picker.css" rel="stylesheet" />
		<link href="css/mui.poppicker.css" rel="stylesheet" />
		<!--<link rel="stylesheet" type="text/css" href="../css/mui.picker.min.css" />-->
		<style>
			.mui-btn {
				font-size: 16px;
				padding: 8px;
				margin: 3px;
			}

			h5.mui-content-padded {
				margin-left: 3px;
				margin-top: 20px !important;
			}

			h5.mui-content-padded:first-child {
				margin-top: 12px !important;
			}

			.ui-alert {
				text-align: center;
				padding: 20px 10px;
				font-size: 16px;
			}

			.underline {
				text-decoration: underline;
			}

			.Warn {
				color: #d54941;
			}
		</style>

	</head>
	<body>
		<header class="mui-bar mui-bar-nav">
			<a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>				
			<div class="mui-action-menu mui-icon  mui-pull-left" id="divLeft" >
				<input type="text" id="txtKeword" value="" placeholder="输入查询内容" style="width: 100px;" onclick="Parse_Query(this)"/>合计：<span id="txtTotal" onclick="Parse_QueryJson()"></span>
			</div>
			<h1 class="mui-title">数据批量录入</h1>
			<div class="mui-action-menu mui-icon  mui-pull-right">
				
				<button id="btnError" class="mui-btn mui-btn-danger" type="button" onclick="Parse_Error_Show()"
					style="top:0px;display:none; line-height: 1;">失败信息</button>&nbsp;&nbsp;
				<button id="btnParse" class="mui-btn mui-btn-danger" type="button" onclick="Parse_Exec()"
					style="top:0px;line-height: 1;">解析</button>&nbsp;&nbsp;
				<button id="btnSave" class="mui-btn mui-btn-primary" type="button" onclick="Parse_Save()"
					style="top:0px;display:none;line-height: 1;">保存</button>
				<button class="mui-btn mui-btn" id="btnClear" type="button" onclick="Parse_ClearAll();"
					style="top:0px;display: none;line-height: 1;">清除解析</button>
			</div>
		</header>
		<div class="mui-content" style="padding-bottom: 60px;">
			<div id="divRecordInput" class="mui-card">
				<ul class="mui-table-view">
					<li class="mui-table-view-cell">
						<div class="mui-collapse-content">
							<form class="mui-input-group">
								<div class="mui-input-row" style="height:70vh;">
									<div>
										<label>内容：</label>
										<textarea id="txtCount" rows="10"
											style="border:solid 1px blue;height:70vh"></textarea>
									</div>
								</div>
								<!--
								<div class="mui-button-row">
									<button id="btnParseAgain" class="mui-btn mui-btn-primary" type="button"
										onclick="Parse_State_Show(true);"
										style="display:none;">继续解析</button>&nbsp;&nbsp;
								</div>
								-->
							</form>
						</div>
					</li>
				</ul>
			</div>

			<div id="divRecord" style="display: none;">
				<ul class="mui-table-view" id="ulRecordList">
				</ul>
			</div>

			<div id="divError" class="mui-input-row" style="display: none;height:70vh;">
				<div>
					<label>解析错误信息：</label>
					<textarea id="txtError" rows="10" style="border:solid 1px blue;height:70vh"></textarea>
				</div>
			</div>
		</div>
		<script src="js/mui.min.js"></script>
		<script src="js/mui.picker.js"></script>
		<script src="js/mui.poppicker.js"></script>
		<script src="js/jquery.min.js"></script>
		<script src="libs/lib.js"></script>
		<script>
			var Model = {};
			var isAdd = true;
			var successDataList = [];
			var errorList = [];

			(function($, doc) {
				mui.init({
					swipeBack: true //启用右滑关闭功能
				});

			})(mui, document);

			function Parse_Exec() {
				if (document.getElementById("btnParse").innerText == "继续解析") {
					Parse_State_Show(true);
					return;
				}

				var param = {
					SiteId: Core.Tool.GetUrlParam("siteId"),
					Text: document.getElementById("txtCount").value
				}
				if (param.Text == "" && successDataList.length > 0) {
					Core.Msg.Confirm("解析内容为空，是否返回查看已解析", null, null, function() {
						Parse_State_Show(false);
					})
					return;
				}
				document.getElementById("txtCount").value = "";
				Core.Post("Record/TextParse", param, function(result, status) {
					//重置错误信息
					errorList = []
					if (result.errorMsg) {
						Core.Msg.Alert(result.errorMsg);
						return;
					}
					if (result.datas.length == 0) {
						Core.Msg.Alert("解析结果为空");
						return;
					}

					//console.log(result);
					var successList = [];
					var list = result.datas;
					for (var i = 0; i < list.length; i++) {
						if (list[i].result == "ParsemResult_Fail") {
							errorList.push(list[i]);
							continue;
						}
						if (list[i].lstTag && list[i].lstTag.length) {
							for (var j = 0; j < list[i].lstTag.length; j++) {
								list[i].lstTag[j].source = list[i].source;
								list[i].lstTag[j].result = list[i].result;
								list[i].lstTag[j].resultTip = list[i].resultTip;
								successDataList.push(list[i].lstTag[j]);
							}
						}
						successList.push(list[i]);
					}
					
					console.log(successDataList);
					if (successDataList.length == 0) {
						document.getElementById("btnError").innerText = "失败信息";
						Parse_Error_Show();
						return;
					}
					Parse_Query();
					Parse_State_Show(false);
				});
			}
			
			var thisDataList = [];
			function Parse_QueryJson(){
				console.log(JSON.stringify(thisDataList));
			}
			function Parse_Query(obj){
				var keyword = "";
				if(obj)
					keyword = obj.value;
				if (successDataList.length == 0) 
					return;
					
				thisDataList = [];
				var html = "";
				var tip = "";
				var total = 0;				
				for (var i = 0; i < successDataList.length; i++) {
					if(keyword && successDataList[i].tagTypeName.indexOf(keyword) == -1 && successDataList[i].name.indexOf(keyword) == -1){
						continue;
					}						
					thisDataList.push(
					{
						tagTypeName: successDataList[i].tagTypeName, 
						name: successDataList[i].name, 
						dataValue: successDataList[i].dataValue,
						source: successDataList[i].source
					});
					
					total += successDataList[i].dataValue;
					tip = "";
					if (successDataList[i].result == "ParsemResult_Warn" && successDataList[i].resultTip) {
						tip = " <span class='Warn'>【警告：" + successDataList[i].resultTip + "】</span>";
					}
				
					html += " <li class='mui-table-view-cell' style='font-style: oblique;' onclick='Record_Edit(" +
						successDataList[i].id + ")'> ";
					html += " 	<div class='mui-table'> ";
					html += " 		<div class='mui-table-cell' style='width:70%'> ";
					html += " 			<h4 class='mui-ellipsis'>" + successDataList[i].tagTypeName + " -【" +
						successDataList[i].name +
						"】 &nbsp;<span class='mui-badge mui-badge-success' style='font-size: 24px;'>" +
						successDataList[i].dataValue + "</span> </h4>";
					html += " 				<h5 class='underline'>来源：" + successDataList[i].source + tip + "</h5>";
					html += " 			</div> ";
					html += " 	<div class='mui-table-cell mui-text-right' style='width:30%'> ";
					html += " 		<span class='mui-h5'>" + successDataList[i].createTime + "</span> ";
					html += " 	</div> ";
					html += " </div>						 ";
					html += "</li> ";
				}
				document.getElementById("ulRecordList").innerHTML = html;
				document.getElementById("txtTotal").innerHTML = total;
			}

			//解析控件显示
			function Parse_State_Show(isParse) {
				if (isParse) {
					//解析
					document.getElementById("btnClear").style.display = "none";
					document.getElementById("btnSave").style.display = "none";
					document.getElementById("btnError").style.display = "none";
					document.getElementById("divLeft").style.display = "none";
					document.getElementById("btnParse").innerText = "解析";

					document.getElementById("divRecordInput").style.display = "";
					document.getElementById("divRecord").style.display = "none";
					document.getElementById("divError").style.display = "none";
					return;
				} else {
					//继续解析
					document.getElementById("btnClear").style.display = "";
					document.getElementById("btnSave").style.display = "";
					document.getElementById("btnError").style.display = "none";
					document.getElementById("divLeft").style.display = "";
					
					document.getElementById("divRecordInput").style.display = "none";
					document.getElementById("divRecord").style.display = "";
					document.getElementById("btnParse").innerText = "继续解析";
					document.getElementById("divError").style.display = "none";
				}

				document.getElementById("btnError").innerText = "失败信息";
				if (errorList.length > 0)
					document.getElementById("btnError").style.display = "";
			}

			//清除所有解析
			function Parse_ClearAll() {
				successDataList = [];
				errorList = [];
				document.getElementById("ulRecordList").innerHTML = "";
			}

			//保存
			function Parse_Save() {
				if (successDataList.length == 0) {
					Core.Msg.Alert("保存不能为空");
					return;
				}
				var siteId = Core.Tool.GetUrlParam("siteId");
				var createUserId = Core.User.GetUser().id;
				var list = [];
				for (var i = 0; i < successDataList.length; i++) {
					list.push({
						siteId: siteId,
						tagId: successDataList[i].id,
						dataValue: successDataList[i].dataValue,
						createUserId: createUserId
					});
				}
				Core.Post("Record/AddList", {
					Param: list
				}, function(result, status) {
					if (result.errorMsg) {
						Core.Msg.Alert(result.errorMsg);
						return;
					}
					Core.Msg.Alert("保存成功");
					setTimeout(function() {
						window.location.href = "record.html?siteId=" + siteId;
					}, 1000)
				});
			}

			function Parse_Error_Show() {
				if (document.getElementById("btnError").innerText == "失败信息") {
					if (errorList.length == 0)
						return;
					document.getElementById("divRecordInput").style.display = "none";
					document.getElementById("btnError").style.display = "";
					document.getElementById("divError").style.display = "";

					document.getElementById("divRecord").style.display = "none";
					document.getElementById("btnClear").style.display = "none";
					document.getElementById("btnSave").style.display = "none";
					document.getElementById("btnParse").style.display = "none";
					document.getElementById("btnError").innerText = "返回解析";

					var html = "";
					for (var i = 0; i < errorList.length; i++) {
						html += "来源：" + errorList[i].source + "【失败原因：" + errorList[i].resultTip + "】\r\n";
					}
					document.getElementById("txtError").innerHTML = html;
				} else {
					document.getElementById("divRecordInput").style.display = "";
					document.getElementById("btnError").style.display = "none";
					document.getElementById("divError").style.display = "none";

					// document.getElementById("divRecord").style.display = "";
					// document.getElementById("btnClear").style.display = "";
					// document.getElementById("btnSave").style.display = "";
					document.getElementById("btnParse").style.display = "";

					//document.getElementById("btnError").innerText = "失败信息";
				}
			}
		</script>
	</body>

</html>